﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace RegattaStarter
{
    public static class SettingsMethods
    {
        private static IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
        private static List<string> flags =  new List<string>();

        public static List<string> GetFlags(bool defaultConfiguration)
        {
            flags = new List<string>();
            string avanadeLogoUrl = "/Images/Avanade_Logo.png";
            string firstFlag = string.Empty;
            string secondFlag = string.Empty;
            string thirdFlag = string.Empty;

            if (defaultConfiguration)
            {
                firstFlag = "/Images/Flags/Papa_Flag.png";
                secondFlag = "/Images/Flags/Oscar_Flag.png";
                thirdFlag = "/Images/Flags/Romeo_Flag.png";
            }
            else 
            {
                try 
                {
                    firstFlag = (string)userSettings["firstFlagSetting"];
                    secondFlag = (string)userSettings["secondFlagSetting"];
                    thirdFlag = (string)userSettings["thirdFlagSetting"];
                }
                catch (Exception ex) 
                {
                    firstFlag = "/Images/Flags/Papa_Flag.png";
                    secondFlag = "/Images/Flags/Oscar_Flag.png";
                    thirdFlag = "/Images/Flags/Romeo_Flag.png";
                }
            }

            flags.Add(firstFlag);
            flags.Add(secondFlag);
            flags.Add(thirdFlag);
            flags.Add(avanadeLogoUrl);

            return flags;
        }

        public static void SaveConfiguration(string flagOrder, string url) 
        {
            switch (flagOrder) 
            {
                case "first":
                    userSettings.Remove("firstFlagSetting");
                    userSettings.Add("firstFlagSetting", url);
                    break;
                case "second":
                    userSettings.Remove("secondFlagSetting");
                    userSettings.Add("secondFlagSetting", url);
                    break;
                case "third":
                    userSettings.Remove("thirdFlagSetting");
                    userSettings.Add("thirdFlagSetting", url);
                    break;
            }

            userSettings.Save();
        }

        public static void SaveCustomConfiguration() 
        {
            try
            {
                string cusomfirstFlag = (string)userSettings["firstFlagSettingCustom"];
                string cusomsecondFlag = (string)userSettings["secondFlagSettingCustom"];
                string cusomthirdFlag = (string)userSettings["thirdFlagSettingCustom"];

                List<string> flags = GetFlags(false);

                if (!string.IsNullOrEmpty(cusomfirstFlag))
                {
                    userSettings.Remove("firstFlagSetting");
                    userSettings.Add("firstFlagSetting", cusomfirstFlag);
                }
                if (!string.IsNullOrEmpty(cusomsecondFlag)) 
                {
                    userSettings.Remove("secondFlagSetting");
                    userSettings.Add("secondFlagSetting", cusomsecondFlag);
                }
                if (!string.IsNullOrEmpty(cusomthirdFlag)) 
                {
                    userSettings.Remove("thirdFlagSetting");
                    userSettings.Add("thirdFlagSetting", cusomthirdFlag);
                }

                userSettings.Save();
            }
            catch (Exception ex) 
            {
            }
        }

        public static void SaveUseDefault(bool userUse) 
        {
            userSettings.Remove("useDefault");
            userSettings.Add("useDefault", userUse);
            userSettings.Save();
        }

        public static bool GetUserDefault ()
        {
            bool useDef = false;
            try
            {
                useDef = (bool)userSettings["useDefault"];
            }
            catch (Exception ex) 
            {
                useDef = false;
            }

            return useDef;
        }
    }
}
